package com.qf.zhijia.zhijiasearch.service.Impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.zhijia.zhijiasearch.pojo.Product;
import com.qf.zhijia.zhijiasearch.service.SearchService;
import com.qf.zhijia.zhijiasearch.vo.TableVo;
import org.apache.commons.beanutils.BeanUtils;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author 岳鹏
 * @Date 2020/9/9
 */
@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    RestHighLevelClient restHighLevelClient;


    //创建索引和type
    String index = "zhijia-index";
    String type = "zhijia-type";
    //json转换
    ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 查询
     * @param pName
     * @return
     * @throws Exception
     */
    @Override
    public String search(String pName) throws Exception {
        List<Product> productList = new ArrayList<>();
        //创建SearchRequest请求
        SearchRequest request = new SearchRequest(index).types(type);
        //创建SearchSourceBuilder
        SearchSourceBuilder builder = new SearchSourceBuilder();
        if (pName != null){
            builder.query(QueryBuilders.matchQuery("pname",pName));
        }
        request.source(builder);

        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        for (SearchHit hit : response.getHits().getHits()){
            Map<String, Object> map = hit.getSourceAsMap();
            Product product = new Product();
            //解析 map
            System.out.println("======================="+map);
//            BeanUtils.populate(product,map);
            String pImage = (String) map.get("pimage");
            String pinfo = (String) map.get("pinfo");
            String pname = (String) map.get("pname");
//            BigDecimal pprice = (BigDecimal) map.get("pprice");
            String pstyle = (String) map.get("pstyle");
            Integer themeId = (Integer) map.get("themeId");
            Integer pid = (Integer) map.get("pid");
            Integer tid = (Integer) map.get("tid");

            product.setPImage(pImage);
            product.setPInfo(pinfo);
            product.setPName(pname);
//            product.setPPrice(pprice);
            product.setPStyle(pstyle);
            product.setThemeId(themeId);
            product.setPId(pid);
            product.setTId(tid);
            productList.add(product);
        }
        //总执行条数
        long totalhis = response.getHits().getTotalHits();

        TableVo tableVo = new TableVo();
        tableVo.setData(productList);
        tableVo.setCount(totalhis);

        String json = objectMapper.writeValueAsString(tableVo);
        return json;
    }

    /**
     * 增加   插入一条数据，同步到ES
     * @param product
     * @throws Exception
     */
    @Override
    public void add(Product product) throws Exception {
        System.out.println("开始执行ES添加。。。。。。。。");
        //1.创建Request请求
        IndexRequest indexRequest = new IndexRequest(index,type,product.getPId().toString());

        //2.准备json
        String json = objectMapper.writeValueAsString(product);
        indexRequest.source(json, XContentType.JSON);

        //3.执行增加
        IndexResponse indexResponse = restHighLevelClient.index(indexRequest,RequestOptions.DEFAULT);

        //4.增加成功
        if ("created".equalsIgnoreCase(indexResponse.getResult().toString())){
            System.out.println("增加成功。。。。。。");
        }
    }


}
